<template>
  <div class="customer-dataedit-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Plus" type="primary" @click="handleEdit">
          添加一个新客户
        </el-button>
        <!-- <el-button :icon="Delete" type="danger" @click="handleDelete">
          批量删除
        </el-button> -->
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.searchKey"
              clearable
              placeholder="请输入客户名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="公海ID"
        prop="CustomerID"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        label="客户全称"
        prop="CustomerAllName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="所在地区"
        prop="CityShow"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="所属行业"
        prop="IndustryShow"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="地址"
        prop="Address"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="电话"
        prop="Telphone"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="客户标记"
        prop="CustomerMarks"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="信息维护" width="100">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="handleEdit(row)">
            信息维护
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="证照维护" width="100">
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="licenseEdit(row)">
            证照维护
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
    <LicenseMsg ref="LicenseMsgRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getAllCustomerList } from '@/api/customer'
  import Edit from '@/views/components/CustomerDataeditEdit'
  import LicenseMsg from '@/views/components/LicenseMsg'

  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'CustomerDataedit',
    components: { Edit, LicenseMsg },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        editRef: null,
        LicenseMsgRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          searchKey: '',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.CustomerID) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getAllCustomerList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      const licenseEdit = (row) => {
        state.LicenseMsgRef.showEdit(row)
        console.log(row, '证照维护')
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        licenseEdit,
        handleEdit,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
